package com.group.eams.controller;






import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.group.eams.pojo.dto.StorageLocationQueryDto;
import com.group.eams.pojo.entity.StorageLocation;
import com.group.eams.service.StorageLocationService;
import com.group.pojo.dto.BatchDeleteDto;
import com.group.result.Result;
import com.group.result.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;




/**
 * 存放位置(StorageLocation)表控制层
 *
 * @author 
 * @since 2024-10-30 12:58:29
 */
@RestController
@RequestMapping("/storageLocation")

public class StorageLocationController  {
    /**
     * 服务对象
     */
    @Autowired
    private StorageLocationService storageLocationService;

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public Result list(StorageLocationQueryDto storageLocationQueryDto){
        Page<StorageLocation> page= new Page<>(storageLocationQueryDto.getPage(), storageLocationQueryDto.getLimit());

        LambdaQueryWrapper<StorageLocation> queryWrapper=new LambdaQueryWrapper<>();

        queryWrapper.like(!StrUtil.isEmpty(storageLocationQueryDto.getName()),
                StorageLocation::getName, storageLocationQueryDto.getName())
                .like(!StrUtil.isEmpty(storageLocationQueryDto.getCode()),
                        StorageLocation::getCode, storageLocationQueryDto.getCode())
                .like(storageLocationQueryDto.getStatus()!=null,StorageLocation::getStatus,storageLocationQueryDto.getStatus());
        Page<StorageLocation> storageLocationPage = storageLocationService.page(page, queryWrapper);
        return Result.buildSuccess(storageLocationPage.getTotal(), storageLocationPage.getRecords());
    }


    @RequestMapping(value = "/findById",method = RequestMethod.GET)
    public Result findById(@RequestParam Long id){
        StorageLocation storageLocation = storageLocationService.getById(id);
        return Result.buildSuccess(storageLocation);
    }


    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    public Result findAll() {
        return Result.buildSuccess(storageLocationService.list());
    }


    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result add(@RequestBody StorageLocation storageLocation){
        storageLocationService.save(storageLocation);
        return Result.buildSuccess();
    }


    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result update(@RequestBody StorageLocation storageLocation){
        LambdaUpdateWrapper<StorageLocation> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(StorageLocation::getId, storageLocation.getId());
        boolean  result = storageLocationService.update(storageLocation, queryWrapper);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);

    }

    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public Result delete(@RequestParam Long id){
        LambdaQueryWrapper<StorageLocation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StorageLocation::getId, id);
        boolean result = storageLocationService.remove(queryWrapper);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/batchRemove",method = RequestMethod.POST)
    public Result batchRemove(@RequestBody BatchDeleteDto batchDeleteDto){

        LambdaQueryWrapper<StorageLocation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(StorageLocation::getId, batchDeleteDto.getIds());
        boolean result = storageLocationService.remove(queryWrapper);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }





}

